<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :modal="false" :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="单位编号" prop="aab001">
            <el-input v-model="form.aab001" style="width: 370px;" :disabled="true" />
            <!--            <unit-rpc></unit-rpc>-->
          </el-form-item>
          <el-form-item label="参保险种" prop="aae140">
            <el-select v-model="form.aae140" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aae140" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="参保日期" prop="aab050">
            <el-date-picker v-model="form.aab050" format="yyyy-MM-dd" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="参保状态" prop="aab316">
            <el-select v-model="form.aab316" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aab316" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费状态" prop="aab051">
            <el-select v-model="form.aab051" placeholder="请选择" style="width: 370px">
              <el-option v-for="item in dict.aab051" :key="item.id" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="缴费开始年月" prop="aae041">
            <el-date-picker v-model="form.aae041" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="缴费截止年月" prop="aae042">
            <el-date-picker v-model="form.aae042" format="yyyy-MM" type="datetime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="经办机构">
            <el-input v-model="form.aab359" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.aae013" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="tableData" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="aaz040" label="参保关系id" fixed="left" />
        <el-table-column prop="aab001" label="单位编号" fixed="left" width="150px" />
        <el-table-column prop="aae140" label="参保险种" :formatter="formatter" />
        <el-table-column prop="aab050" label="参保日期" :formatter="formatDate" />
        <el-table-column prop="aab316" label="参保状态" :formatter="formatter" />
        <el-table-column prop="aab051" label="参保状态" :formatter="formatter" />
        <el-table-column prop="aae041" label="缴费开始年月" :formatter="formatDate" width="150px" />
        <el-table-column prop="aae042" label="缴费截止年月" :formatter="formatDate" width="150px" />
        <el-table-column prop="aab359" label="经办机构" />
        <el-table-column prop="aae013" label="备注" />
        <el-table-column v-if="checkPer(['admin','basicAb02:edit','basicAb02:del'])" label="操作" width="150px" align="center" fixed="right">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBasicAb02 from '@/views/ynsb_comm/ab02/basicAb02'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { aab001: null, aae140: null, aab050: null, aab316: null, aab051: null, aae041: null, aae042: null, aab359: null, aae013: null }
export default {
  name: 'BasicAb02',
  components: { pagination, crudOperation, udOperation },
  props: {
    aab001: {
      type: String,
      required: true
    }
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位参保信息采集', url: 'api/basicAb02', idField: 'aab001', sort: 'aab001,desc', crudMethod: { ...crudBasicAb02 }})
  },
  // 数据字典
  dicts: ['aae140', 'aab316', 'aab051'],
  data() {
    return {
      permission: {
        add: ['admin', 'basicAb02:add'],
        edit: ['admin', 'basicAb02:edit'],
        del: ['admin', 'basicAb02:del']
      },
      rules: {
        aab001: [
          { required: true, message: '单位编号不能为空', trigger: 'blur' }
        ]
      },
      tableData: [] // 单位参保信息
    }
  },
  watch: {
    'crud.status.cu'(val) {
      if (this.crud.status.cu === '1') {
        this.form.aab001 = this.aab001
      } else {
        crudBasicAb02.getAb02({ aab001: this.aab001 }).then(res => {
          this.tableData = res
        })
      }
    }
  },
  methods: {
    formatter(row, column, cellValue) {
      // 根据 aae140 的值进行转码
      const key = column.property
      const data = this.dict[key]
      console.log(data, 'data')
      const dataList = data.filter(item => item.value === cellValue)
      if (dataList !== null && dataList !== [] && dataList !== undefined) {
        return dataList[0].label
      }
    },
    getAb02s() {
      crudBasicAb02.getAb02({ aab001: this.aab001 }).then(res => {
        this.tableData = res
      })
    },
    // 日期格式转换
    formatDate(row, column, cellValue) {
      // 格式化日期为 YYYY-MM-DD
      if (cellValue !== null && cellValue !== undefined) {
        const date = new Date(cellValue)
        const year = date.getFullYear()
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        const day = date.getDate().toString().padStart(2, '0')
        return `${year}-${month}-${day}`
      } else {
        return '--'
      }
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
